/**
 * Created by xiangyufang on 2016/11/10.
 */
;(function ($, window, document, undefined) {

    //应急资源类别
    var $type = $('#JQ_type').select3();
    DyyBase.ajax(ambientApi.url_common_form_type('AE_EMERGENCY_RESOURCE_TYPE'), {}, function(dataset){
        var json = DyyBase.getSelect(dataset);
        $type.add(json);
    });

    //表格
    var $table = $("#JQ_table");
    //查询
    var $search = $("#JQ_search");
    //地图查询
    var $mapSearch = $("#JQ_map_search");
    //新建
    var $create = $("#JQ_create");
    //导出
    var $export = $("#JQ_export");
    //切换地图
    var $toggle_map = $("#JQ_toggle_map_span");
    //切换表格
    var $toggle_list = $("#JQ_toggle_list_span");


    // 查询对象
    var filter = {
        getSearch: function (param) {
            var data = {};
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            //资源类别参数
            data.type = function () {
                var v = $type.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            return data;
        },
        // 获得导出条件
        getExport: function(){
            return filter.getSearch();
        }
    };


    // 加载表格
    var DTTABLE = $table.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(ambientApi.url_ae_emergency_search(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
            var th = $table.find('th');
            var empty = $table.find('td.dataTables_empty');
            empty.attr('colspan',th.length);
        },
        'columns': [
            {
                data: 'name',
                render: function (data,type,row) {
                    return '<span title="' + data + '">' + data + '</span><input type="hidden" name="gisId" value="' + row.gisid + '">';
                }
            },
            {
                data: 'type',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'province',
                render: function (data,type,row) {
                    if(data.indexOf('省')>0){
                        if(row.city == '市辖区' || row.city == '辖区'){
                            if(row.district == '其他'){
                                return '<span title="'+ data + row.town + row.village + '">'+ data + row.town + row.village + '</span>';
                            }else{
                                return '<span title="'+ data + row.district + row.town + row.village + '">'+ data + row.district + row.town + row.village + '</span>';
                            }
                        }else{
                            return '<span title="' + data + row.city + row.district + row.town +  row.village + '">' + data + row.city + row.district + row.town + row.village + '</span>';
                        }
                    }else{
                        return '<span title="' + row.city + row.district + row.town +  row.village + '">' + row.city + row.district + row.town + row.village + '</span>';
                    }
                }
            },
            {
                data: 'contact',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'telephone',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    var state = row.dataStated;
                    if(state == '20'){
                        return '<i data-a="' + data + '" class="xyf-shenhe-no-icon"></i>';
                    }else if(state == '05'){
                        return '<i data-a="' + data + '" class="zy-list-icon-examine" title="审核"></i>';
                    }else if(state == '06'){
                        return '<i data-a="' + data + '" class="zy-shenhe-no-icon"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    var state = row.dataStated;
                    if(state == '20'){
                        return '<i data-a="' + data + '" class="zy-record-add-icon disabled"></i>';
                    }else if(state == '05'){
                        return '<i data-a="' + data + '" class="zy-record-add-icon" title="编辑"></i>';
                    }else if(state == '06'){
                        return '<i data-a="' + data + '" class="zy-record-add-icon" title="编辑"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    var state = row.dataStated;
                    if(state == '20'){
                        return '<i data-a="' + data + '" class="zy-list-icon-delete disabled"></i>';
                    }else if(state == '05'){
                        return '<i data-a="' + data + '" class="zy-list-icon-delete" title="删除"></i>';
                    }else if(state == '06'){
                        return '<i data-a="' + data + '" class="zy-list-icon-delete" title="删除"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data) {
                    return '<i data-id="' + data + '" class="zy-details-icon" title="查看详情"></i>';
                }
            }
        ]
    });

    //查询
    $search.click(function(){
        if(DTTABLE){
            DTTABLE.draw();
        }
    });
    //新建
    $create.click(function(){
        var id = '';
        var type = 'EmergencyResource';
        window.location.href = ambientApi.url_ae_addEdit(id,type);
    });

    //导出
    $export.click(function(){
        $(this).EXPT({
            type: 'name',
            name: '应急资源信息表',
            param: filter.getExport,
            server: DyyBase.root_path + '/ae/emergencyResource/export.json'
        });
    });

    // 审核
    $table.on('click', 'i.zy-list-icon-examine', function () {
        var $this = $(this);
        var id = $this.attr('data-a');
        var type = 'EmergencyResource';
        window.location.href = ambientApi.url_ae_examine(id,type);
    });

    // 编辑
    $table.on('click', 'i.zy-record-add-icon', function () {
        var $this = $(this);
        var id = $this.attr('data-a');
        var type = 'EmergencyResource';
        if(!($this.hasClass('disabled'))) {
            window.location.href = ambientApi.url_ae_addEdit(id, type);
        }
    });

    // 删除
    $table.on('click', 'i.zy-list-icon-delete', function (){
        var $this = $(this);
        var id = $this.attr('data-a');
        if(!($this.hasClass('disabled'))) {
            $this.MSG({
                type: 'confirm',
                height: 230,
                content: '您确认要删除此数据吗?',
                yesBtnTxt: '确认',
                yesBtnAct: function () {
                    DyyBase.ajax(ambientApi.url_ae_emergency_delete(), {id: id}, function (dataSet) {
                        if (dataSet.bool) {
                            $(this).MSG({
                                type: 'success',
                                height: 300,
                                content: '删除成功',
                                yesBtnTxt: '确认',
                                yesBtnAct: function () {
                                    if (DTTABLE) {
                                        DTTABLE.draw();
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }
    });

    // 详情
    $table.on('click', 'i.zy-details-icon', function () {
        var $this = $(this);
        var Id = $this.attr('data-id');
        DyyBase.ajax(ambientApi.url_ae_emergency_examine(), {id:Id,type:'show'}, function(dataSet){
            if(dataSet.bool){
                if(dataSet.result.imgList){
                    var imgData = DyyBase.getImagePath(dataSet.result.imgList);
                    DyyBase.showDivDetails($this, DTTABLE, $table, ambientTemplate['resource'], imgData);
                }else {
                    DyyBase.showDivDetails($this, DTTABLE, $table, ambientTemplate['resource']);
                }
            }
        });
    });

    //地图
    $toggle_map.click(function(){
        DyyBase.toggleListMap('map');
        var gisJsonArr;
        var input = $table.find('input[name="gisId"]');
        if(input.length>0){
            gisJsonArr = [{typeName:'PicAeEmergencyResource',gisIdArray:[]}];
            input.each(function(){
                var $this = $(this);
                var v = $this.val();
                if(v && v!='-'){
                    gisJsonArr[0].gisIdArray.push(v);
                }
            });
        }else{
            gisJsonArr = [];
        }
        //console.log(gisJsonArr);
        DyyBase.aeMap('JQ_dyMap',gisJsonArr);
    });

    //表格
    $toggle_list.click(function(){
        DyyBase.toggleListMap('list');
        if(DTTABLE){
            DTTABLE.draw();
        }
    });

    //地图查询
    $mapSearch.click(function(){
        var param = filter.getSearch();
        DyyBase.ajax(ambientApi.url_ae_emergency_search(), param, function (dataset) {
            if(dataset.bool){
                var gisJsonArr;
                var returnData = dataset.result.list;
                if(returnData && returnData.length>0){
                    gisJsonArr = [{typeName:'PicAeEmergencyResource',gisIdArray:[]}];
                    for(var i=0;i<returnData.length;i++){
                        if(returnData[i].gisid){
                            gisJsonArr[0].gisIdArray.push(returnData[i].gisid);
                        }
                    }
                }else{
                    gisJsonArr = [];
                }
                //console.log(gisJsonArr);
                DyyBase.aeMap('JQ_dyMap',gisJsonArr);
            }
        });
    });

})(jQuery, window, document);